package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.Threads;
import com.ruoyi.system.domain.MaterialEntity;
import com.ruoyi.system.domain.MaterialLogEntity;
import com.ruoyi.system.domain.TProjectAim;
import com.ruoyi.system.mapper.MaterialLogMapper;
import com.ruoyi.system.mapper.MaterialMapper;
import com.ruoyi.system.mapper.TProjectAimMapper;
import com.ruoyi.system.service.MaterialService;
import lombok.var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;


@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, MaterialEntity> implements MaterialService {


    @Autowired
    private MaterialMapper materialMapper;


    @Autowired
    private MaterialLogMapper materialLogMapper;

    private static final Logger logger = LoggerFactory.getLogger(MaterialServiceImpl.class);




    public MaterialEntity getByCode(String materialCode) {
        return materialMapper.getByMaterialCode(materialCode);
    }

    @Transactional
    public boolean upsertMaterial(MaterialEntity materialEntity ) {

        // 如果材料id 不存在 就添加

        // 使用材料的 code 唯一标识和关联
        var materialCode = materialEntity.getMaterialCode();
        var existMaterialEntity = getByCode(materialCode);

        // 如果不存在直接更新
        if (existMaterialEntity == null) {
            return  saveOrUpdate(materialEntity);
        }
        // 直接更新字段
        materialEntity.setId(existMaterialEntity.getId());

        return  saveOrUpdate(materialEntity);
    }


}
